import pymysql
import random
import json

# 数据库连接配置
conn = pymysql.connect(
    host='gateway01.ap-southeast-1.prod.aws.tidbcloud.com',
    port=4000,
    user='LdC71APSKJWjErP.root',
    password='4WJWO7PiBBcPwAvG',
    database='github_sample',
    ssl={'ca': '/etc/ssl/certs/ca-certificates.crt'},
    charset='utf8mb4',
    cursorclass=pymysql.cursors.DictCursor
)

# 准备数据
tag_options = [
    ['电子', '科技', '新品'],
    ['家居', '生活'],
    ['服装', '时尚', '促销', '2023']
]

values_array_options = [
    ['电子', '科技', '新品'],
    ['家居', '生活'],
    ['服装', '时尚', '促销', '2023']
]

numeric_val = 100
text_val = "raise_mysql_exceptio"

try:
    for _ in range(50):  # 执行5次批量插入
        # 生成批量数据（10000条）
        batch_data = []
        for _ in range(10000):
            tags = json.dumps(tag_options)  # 随机选择标签
            values = json.dumps(values_array_options)  # 随机选择值
            batch_data.append((numeric_val, text_val, tags, values))
        
        with conn.cursor() as cursor:
            cursor.executemany(
                """
                INSERT INTO test_array_performance (
                    numeric_field, text_field, tags, values_array
                ) VALUES (%s, %s, %s, %s)
                """,
                batch_data
            )
        conn.commit()
        print(f"第 {_+1} 次批量插入成功！插入了 {len(batch_data)} 条数据")
        
except Exception as e:
    conn.rollback()
    print(f"插入失败: {e}")
finally:
    conn.close()